package com.amz.analyzer.auth.controller;

import com.amz.analyzer.req.LoginReq;
import com.amz.analyzer.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Freddy
 * @since 2020-08-20
 */
@Slf4j
@Api(tags = "认证相关服务")
@RestController
@RequestMapping("/auth")
public class AuthController {

    @PostMapping(value = "/login")
    @ApiOperation(value = "/login", notes = "系统登录")
    public Result login(@Validated @RequestBody LoginReq loginReq) {

        if ("admin".equals(loginReq.getUsername()) && "admin123".equals(loginReq.getPassword())) {
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("username", loginReq.getUsername());
            userInfo.put("token", "12345678");
            userInfo.put("name", "管理员");
            userInfo.put("introduction", "I am a super administrator");
            userInfo.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");

            return Result.success(userInfo);
        }

        return Result.failure("登录失败！");

    }

    @GetMapping(value = "/get-user")
    @ApiOperation(value = "/get-user", notes = "获取用户信息")
    public Result getUserInfo(String token) {
        if ("12345678".equals(token)) {
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("username", "admin");
            userInfo.put("token", "12345678");
            userInfo.put("name", "管理员");
            userInfo.put("introduction", "I am a super administrator");
            userInfo.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");

            return Result.success(userInfo);
        }
        return Result.failure("登录失败！");
    }

    @PostMapping(value = "/logout")
    @ApiOperation(value = "/logout", notes = "退出登录")
    public Result logout(String token) {
        return Result.success("操作成功！");
    }


}
